package webserver;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Logger manages the output of logging informations
 * @author Andi
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.B5633764-16C7-9573-868A-6D33CE0FD15D]
// </editor-fold> 
public class Logger {

	// Static Declarations
	private static FileWriter serverLog = null;

	/**
	 * Logger Default Constructor
	 * Private for Singleton Design-Pattern
	 */
	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.AF9593A7-2ED6-0157-AC52-1ABCFA524654]
	// </editor-fold>
	private Logger() {
	}

	/**
	 * Write the given text to the log
	 * @param text
	 */
	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.B7F5A72B-2132-FD1F-296C-5C025D7E8768]
	// </editor-fold>
	public static void writeToLog(String text) {
		System.out.println(text);
	}

	/**
	 * Write the given String to the servers global log file
	 * @param text (should be in combined logfile format!)
	 * @see HTTP10Request#getLogFileEntry()
	 * @see HTTP11Request#getLogFileEntry()
	 */
	public static void writeToServerLog(String text) {
		// Generate FileWriter
		if (serverLog == null) {
			try {
				serverLog = new FileWriter(new File("log.txt"));
			} catch (IOException f) {
				Logger.writeToLog("IO Exception opening Server log: " + f.toString());
			}
		}

		try {
			serverLog.write(text + System.getProperty("line.separator"));
			serverLog.flush();
		} catch (IOException ex) {
			Logger.writeToLog("IO Exception wrting to Server Log: " + ex.toString());
		}
	}
}

